/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2016年11月6日
 * <修改描述:>
 */
package com.tx.core.ddlutil.builder.alter;

import com.tx.core.ddlutil.builder.alter.impl.H2AlterTableDDLBuilder;
import com.tx.core.ddlutil.builder.alter.impl.MysqlAlterTableDDLBuilder;
import com.tx.core.ddlutil.builder.alter.impl.PostgreSQLAlterTableDDLBuilder;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.starter.component.DatabaseTypeEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册工厂实例<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2016年11月6日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class AlterTableDDLBuilderFactoryRegistry {

    /**
     * 类型和Factory的映射
     */
    protected static Map<DatabaseTypeEnum, AlterTableDDLBuilderFactory> type2factoryMap = new HashMap<>();

    static {
        AlterTableDDLBuilderFactoryRegistry.registeFactory(
                new MysqlAlterTableDDLBuilder());
        AlterTableDDLBuilderFactoryRegistry.registeFactory(
                new H2AlterTableDDLBuilder());
        AlterTableDDLBuilderFactoryRegistry.registeFactory(
                new PostgreSQLAlterTableDDLBuilder());
    }

    /**
     * 注册工厂实例<br/>
     * <功能详细描述>
     *
     * @param factory [参数说明]
     * @return void [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static void registeFactory(AlterTableDDLBuilderFactory factory) {
        AssertUtils.notNull(factory, "factory is null.");

        AssertUtils.isTrue(!type2factoryMap.containsKey(factory.getDatabaseType()),
                "duplicat dataSourceType:{}",
                new Object[]{factory.getDatabaseType()});
        type2factoryMap.put(factory.getDatabaseType(), factory);
    }

    /**
     * 根据数据库类型获取数据库DDL构建器实例<br/>
     * <功能详细描述>
     *
     * @param dataSourceType
     * @return [参数说明]
     * @return CreateTableDDLBuilder [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static AlterTableDDLBuilderFactory getFactory(
            DatabaseTypeEnum dataSourceType) {
        AssertUtils.notNull(dataSourceType, "dataSourceType is null.");

        AlterTableDDLBuilderFactory builder = type2factoryMap.get(dataSourceType);
        AssertUtils.notNull(builder,
                "builder is not exist.dataSourceType:{}",
                new Object[]{dataSourceType});

        return builder;
    }
}
